const env = require('config.js'); //配置文件，在这文件里配置你的OSS keyId和KeySecret,timeout:87600;
const signature  =  require("./signature.js");
import { getSpecifyTxt } from '../util'

/*
 *上传文件到阿里云oss
 *@param - filePath :图片的本地资源路径
 *@param - dir:表示要传到哪个目录下
 *@param - successc:成功回调
 *@param - failc:失败回调
 */ 
const uploadFile = function (filePath) {
    const { suffix } = getSpecifyTxt(filePath), aliyunFileKey = 'front-course/h5/'+ new Date().getTime() + Math.floor(Math.random() * 150) + suffix;
  
  const aliyunServerURL = env.uploadImageUrl;//OSS地址，需要https

 const formData = {
    key:aliyunFileKey,
    'success_action_status': '200',
    ...signature.getFormDataParams()
    }
    console.log(formData,'uploadFile')
  wx.uploadFile({
    url: aliyunServerURL,//开发者服务器 url
    filePath: filePath,//要上传文件资源的路径
    name: 'file',//必须填file
    formData: formData,
    success: function (res) {
      console.log(res)
      if (res.statusCode != 200) {
        // failc(new Error('上传错误:' + JSON.stringify(res)))
        return;
      }
    //   successc(aliyunServerURL+aliyunFileKey);
    },
    fail: function (err) {
      err.wxaddinfo = aliyunServerURL;
      console.log(err,'上传错误');
    //   failc(err);
    },
  })
}




module.exports = uploadFile;